package com.ceba.test.modules.sign.service;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.ceba.base.helper.LoggerHelper;
import com.ceba.cebautils.DateUtil;
import com.ceba.cebautils.StringUtil;
import com.ceba.test.base.enums.QrFileUploadFlagEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ceba.test.base.configs.CebaConfig;
import com.ceba.base.web.service.IBaseService;
import com.ceba.test.modules.sign.dao.SignDao;
import com.ceba.test.modules.statistic.entity.ReportSign;


public class SignService extends IBaseService {

	private static Logger logger = LoggerFactory.getLogger(SignService.class);


	private SignDao signDao;

	/*
	 * 获取证书剩余天数，少于30天时显示
	 */
	public  String getValideDayOfLicense() {

		try {
			String signValid = CebaConfig.SIGN_VALIDITY;
			if (StringUtil.isEmptyString(signValid)) {
				return "";
			}
			Date signD = DateUtil.parseDate(signValid);
			Date now = new Date();
			long n = signD.getTime() - now.getTime();
			int d = 0;
			if (n > 0) {
				d = (int) (n / (1000 * 60 * 60 * 24));
			} else {
				d = -1;
			}
			if (d <= 30 && d >= 0) {
				return d + "";
			} else if (d < 0) {
				return "over";
			}
		} catch (Exception e) {
			LoggerHelper.error(logger, e, "");
		}
		return "";
	}

	
	/**
	 * 统计所有已签名的报告
	 * 
	 * @param request
	 * @return
	 */
	public String sumAllSign(HttpServletRequest request) {
		return signDao.sumAllSign(request);
	}

	
	
	/**
	 * 添加一个数字证书签名记录
	 * 
	 * @return
	 */
	public Boolean addSignRecord(ReportSign  parm)
			throws Exception {
		return signDao.addSignRecord(parm);
	}

	/**
	 * 根据二维码上传状，获取签名文件
	 * @param qrFileUploadFlag
	 * @return
	 * @throws Exception
	 */
	public List<ReportSign> getReportSign(QrFileUploadFlagEnum qrFileUploadFlag) throws Exception {
		return signDao.getReportSign(qrFileUploadFlag);
	}

	/**
	 * 更新签名报告的二维码上传状态
	 * @param id
	 * @param qrFileUploadFlag
	 * @return
	 * @throws Exception
	 */
	public boolean updateReportSign(String id, QrFileUploadFlagEnum qrFileUploadFlag)  throws Exception {
		return signDao.updateReportSign(id, qrFileUploadFlag);
	}

	/**
	 * 获取所有签名报告个数
	 * 
	 * @return
	 */
	public long getAllSignReports() throws Exception {
		return signDao.getAllSignReports();
	}
	
	/**
	 * 获取所有签名报告个数
	 * 
	 * @return
	 */
	public long getCusUserAllSignReports(String userId,int editionType) throws Exception {
		return signDao.getCusUserAllSignReports(userId,editionType);
	}

	/**
	 * 获取今年所有签名报告个数
	 * 
	 * @return
	 */
	public long getYearSignReports() throws Exception {
		return signDao.getYearSignReports();
	}

	/**
	 * 获取所有签名附件个数
	 * 
	 * @return
	 */
	public long getAllSignOReports() throws Exception {
		return signDao.getAllSignOReports();
	}

	/**
	 * 获取今年所有签名附件个数
	 * 
	 * @return
	 */
	public long getYearSignOReports() throws Exception {
		return signDao.getYearSignOReports();
	}

	/**
	 * 获取前一天所有签名报告个数
	 * 
	 * @return
	 */
	public  long getLastDayReportSign() throws Exception {
		return signDao.getLastDayReportSign();
	}

	/**
	 * 获取前一天所有签名附件个数
	 * 
	 * @return
	 */
	public  long getLastDayAttachSign() {
		return signDao.getLastDayAttachSign();
	}
	
	
	

	public SignDao getSignDao() {
		return signDao;
	}

	public void setSignDao(SignDao signDao) {
		this.signDao = signDao;
	}

	public  void signTest(int count) throws  Exception{
		signDao.signTest(count);
	}

	public int queryFileCount(int count) throws  Exception{
		return signDao.queryFileCount(count);
	}


}
